<?php

class Admin_operation_log_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = "admin_operation_log";
    }

    /*
     * 添加
     */

    public function addData($data) {
        $outArr = array();
        if (!empty($data)) {
            $outArr = $this->db->insert($this->tableName, $data);
            $outArr = $this->db->insert_id();
        }
        return $outArr;
    }

    /*
     * 编辑
     */

    public function updateData($paramArr = array()) {
        $optionArr = array(
            "fieldArr" => array(),
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        if (!empty($fieldArr)) {
            $outArr = $this->db->update($this->tableName, $fieldArr, $whereArr);
        }
        return $outArr;
    }

    /*
     * 删除
     */

    public function delData($paramArr = array()) {
        $optionArr = array(
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        $outArr = $this->db->delete($this->tableName, $whereArr);
        return $outArr;
    }

    /*
     * 得到
     */

    public function getData($where = '', $page = 1, $pageCount = 20, $orderBy = "ORDER BY id DESC", $fields = '*') {

        $result = array();
        $sql = "SELECT " . $fields . " FROM " . $this->tableName . " " . $where . " " . $orderBy;
        if ($pageCount > 0) {
            $sql .= " LIMIT " . (($page - 1) * $pageCount) . ", " . $pageCount;
        }
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    /*
     * 得到条数
     */

    function getDataCount($where = '') {
        $num = 0;
        $sql = "SELECT COUNT(*) AS num FROM " . $this->tableName . " " . $where . " LIMIT 1";
        $query = $this->db->query($sql);
        if ($query) {
            $res = $query->result_array();
            $num = $res[0]["num"];
        }
        return $num;
    }

    public function getDataById($id) {
        $outArr = array();
        $outArr = $this->getData("WHERE id=" . $id);
        if (!empty($outArr)) {
            $outArr = $outArr[0];
        }
        return $outArr;
    }

    /*
     * 增加LOG记录
     */

    public function addOperationLog($table_id = 0, $rec_id = 0, $action_name, $content = []) {

        $data = [
            "table_id" => $table_id,
            "rec_id" => $rec_id,
            "action_name" => $action_name,
            "content" => jsonEncode($content, JSON_UNESCAPED_UNICODE),
            "admin_id" => $this->session->admin_id,
            "client_ip" => get_ip(),
            "created_at" => time(),
            "updated_at" => time(),
        ];
        $result = $this->addData($data);
        return $result;
    }

}
